package cn.tedu.lucky.draw.server.job.config;

import cn.tedu.lucky.draw.server.job.processor.ActivityExpirationProcessor;
import cn.tedu.lucky.draw.server.protocol.po.Activity;
import cn.tedu.lucky.draw.server.repository.ActivityRepository;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.data.RepositoryItemReader;
import org.springframework.batch.item.data.RepositoryItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Sort;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;

@Configuration
public class ActivityExpirationConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private ActivityRepository activityRepository;

    @Bean
    public ItemReader<Activity> activityExpirationReader() {
        RepositoryItemReader repositoryItemReader = new RepositoryItemReader();
        repositoryItemReader.setRepository(activityRepository);
        repositoryItemReader.setMethodName("findByStatusAndEndTimeBefore");
        HashMap<String, Sort.Direction> sorts = new HashMap<>();
        sorts.put("id", Sort.Direction.DESC);
        repositoryItemReader.setSort(sorts);
        //当前status如果为1 表示活动已启用
        repositoryItemReader.setArguments(Arrays.asList(1, LocalDateTime.now()));
        return repositoryItemReader;
    }

    @Bean
    public ActivityExpirationProcessor activityExpirationProcessor() {
        return new ActivityExpirationProcessor();
    }

    @Bean
    public ItemWriter<Activity> activityExpirationWriter(){
        RepositoryItemWriter<Activity> repositoryItemWriter=new RepositoryItemWriter<>();
        repositoryItemWriter.setRepository(activityRepository);
        repositoryItemWriter.setMethodName("save");
        return repositoryItemWriter;
    }

    //定义步骤
    @Bean
    public Step activityExpirationStep(){
        return stepBuilderFactory.get("activityExpirationStep")
                .<Activity,Activity>chunk(100)
                .reader(activityExpirationReader())
                .processor(activityExpirationProcessor())
                .writer(activityExpirationWriter())
                .build();
    }

    //定义作业
    @Bean
    public Job  activityExpirationJob(){
        return jobBuilderFactory.get("activityExpirationJob")
                .start(activityExpirationStep())
                .incrementer(new RunIdIncrementer())
                .build();
    }
}
